package io.xuan.modules.app.controller;

import io.xuan.common.utils.R;
import io.xuan.modules.app.entity.EvaluateEntity;
import io.xuan.modules.app.service.EvaluateService;
import io.xuan.modules.app.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("app/order")
@Api(tags = "订单处理")
public class AppOrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private EvaluateService evaluateService;

    @PostMapping("/updateEvaluate")
    public R updateEvaluate(EvaluateEntity evaluateEntity) {
        // 更新评价
        if (evaluateService.saveEvaluate(evaluateEntity)) return R.ok();
        else return R.error();
    }

    @GetMapping("/userEvaluate")
    public R userEvaluate(@RequestParam("uid") long uid) {
        return R.ok().put("evaluate", evaluateService.queryByUserId(uid));
    }

    @GetMapping("/driverEvaluate")
    public R driverEvaluate(@RequestParam("uid") long uid) {
        return R.ok().put("evaluate", evaluateService.queryByDriverId(uid));
    }

    @GetMapping("/newOrder")
    public R newOrder(@RequestParam("uid") int uid,
                      @RequestParam("driveId") int driveId,
                      @RequestParam("peopleLat") Double peopleLat,
                      @RequestParam("peopleLng") Double peopleLng) {
        if (orderService.saveOrder(uid, driveId, peopleLat, peopleLng)) return R.ok();
        else return R.error();
    }

    @GetMapping("/userOrder")
    public R userOrder(@RequestParam("uid") long uid) {
        return R.ok().put("order", orderService.queryByUserId(uid));
    }

    @GetMapping("/driverOrder")
    public R driverOrder(@RequestParam("uid") long uid) {
        return R.ok().put("order", orderService.queryByDriveId(uid));
    }

    @PutMapping("/updateOrder")
    public R updateOrder(@RequestParam("oid") String oid,
                         @RequestParam("status") int status) {
        if (orderService.updateOrder(status, oid)) return R.ok();
        else return R.error();
    }

    @GetMapping("/getOrder")
    public R getAllOrder() {
        return R.ok().put("order", orderService.allOrder());
    }

    @GetMapping("getEvaluate")
    public R getAllEvaluate() {
        return R.ok().put("evaluate", evaluateService.allEvaluate());
    }

}
